package com.ask.forMe.langchain4j.config;

import com.ask.forMe.langchain4j.store.MongoChatMemoryStore;
import com.ask.forMe.langchain4j.store.MongoRedisChatMemoryStore;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 聊天记忆配置
 */
@Configuration
public class ChatMemoryConfig {
    /**
     * 普通聊天记忆，无持久化
     *
     * @return
     */
    @Bean
    ChatMemory chatMemory() {
        return MessageWindowChatMemory.withMaxMessages(10);
    }

    /**
     * 支持对话隔离的ChatMemoryProvider
     *
     * @return
     */
    @Bean
    ChatMemoryProvider chatMemoryProviderNoDuration() {
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .build();
    }


    @Autowired
    private MongoRedisChatMemoryStore mongoRedisChatMemoryStore;

    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    /**
     * 聊天记忆持久化到至MongoDB
     *
     * @return
     */
    @Bean
    ChatMemoryProvider chatMemoryProvider2Mongo() {
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(mongoRedisChatMemoryStore)
                .build();
    }
}
